package com.lenovo.smarttraffic.ui.fragment;

import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.wjy.Bean_User1;
import com.lenovo.smarttraffic.wjy.Bean_sencedata;
import com.lenovo.smarttraffic.wjy.Bean_weathdata;
import com.lenovo.smarttraffic.wjy.Interface_sence;
import com.lenovo.smarttraffic.wjy.Interface_weath;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * @author Amoly
 * @date 2019/4/11.
 * description：设计页面
 */
public class DesignFragment extends BaseFragment {
    private ImageView typeimage;
    private TextView daytemptext;
    private TextView typetext;
    private TextView timetext;
    private TextView pmtext;
    private TextView co2text;
    private TextView temptext;
    private TextView humitext;
    private PieChart designPieChart;
    private TextView tips;
    private static DesignFragment instance = null;

    int color[]={Color.RED,Color.GREEN,Color.BLUE};
    List<PieEntry> yV=new ArrayList<>();
    int a = 0;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            initdata2();
            yV.clear();
            handler.postDelayed(runnable,3000);
        }
    };
    public static DesignFragment getInstance() {
        if (instance == null) {
            instance = new DesignFragment();
        }
        return instance;
    }


    @Override
    protected View getSuccessView() {
        View view = getLayoutInflater().inflate(R.layout.wjy_design_layout,null,false);
        initview(view);
        inittipsdata();
        initpieset();
        initdata1();
        initdata2();
        handler.postDelayed(runnable,3000);
        return view;
    }
    private void initpieset() {
        designPieChart.setHighlightPerTapEnabled(true);
        designPieChart.setDrawCenterText(true);
        designPieChart.setCenterText("分析");
        designPieChart.setDescription(null );
        designPieChart.setHoleRadius(30);
    }

    private void initdata2() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.110:8088/transportservice/action/").addConverterFactory(GsonConverterFactory.create()).build();
        Interface_sence interface_sence = retrofit.create(Interface_sence.class);
        Call<Bean_sencedata> user1 = interface_sence.getCall(new Bean_User1("user1"));
        user1.enqueue(new Callback<Bean_sencedata>() {
            @Override
            public void onResponse(Call<Bean_sencedata> call, Response<Bean_sencedata> response) {
                if (response.code()==200){
                    int pm25243 = response.body().get_$Pm25145();
                    int co2 = response.body().getCo2();
                    int humidity = response.body().getHumidity();
                    int temperature = response.body().getTemperature();
                    pmtext.setText("PM2.5浓度："+pm25243+"");
                    humitext.setText("湿度："+humidity+"");
                    temptext.setText("温度："+temperature+"");
                    co2text.setText("二氧化碳浓度："+co2+"");

                    yV.add(new PieEntry(pm25243,"PM2.5"));
                    yV.add(new PieEntry(temperature,"温度"));
                    yV.add(new PieEntry(humidity,"湿度"));


                    PieDataSet dataSet = new PieDataSet(yV,"");
                    dataSet.setColors(color);
                    dataSet.setDrawValues(true);
                    designPieChart.setData(new PieData(dataSet));
                    designPieChart.notifyDataSetChanged();
                    designPieChart.invalidate();
                    inittipsdata();
                }
            }

            @Override
            public void onFailure(Call<Bean_sencedata> call, Throwable t) {
                Toast.makeText(getContext(),"error",Toast.LENGTH_SHORT).show();

            }
        });
    }
    public void inittipsdata(){
        a++;
        if (a == 0) {
            tips.setText("易感人群应适当减少室外活动,建议穿短袖衬衫、单裤等服装");
        }
        if (a == 1) {
            tips.setText("空气质量非常好，非常适合户外活动，趁机出去多呼吸新鲜空气");
        }
        if (a == 2) {
            tips.setText("温度低，风较大，较易发生感冒，注意防护,建议穿长袖衬衫、单裤等服装");
        }
        if (a == 3) {
            tips.setText("气候适宜，推荐您进行户外运动,辐射较弱，涂擦SPF12~15、PA+护肤品");
        }
        if (a == 4) {
            tips.setText("尽量减少外出，需要涂抹高倍数防晒霜,建议穿短袖衬衫、单裤等服装");
            a = 0;
        }
    }

    private void initdata1() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.110:8088/transportservice/action/").addConverterFactory(GsonConverterFactory.create()).build();
        Interface_weath interface_weath = retrofit.create(Interface_weath.class);
        Call<Bean_weathdata> user1 = interface_weath.getCall(new Bean_User1("user1"));
        user1.enqueue(new Callback<Bean_weathdata>() {
            @Override
            public void onResponse(Call<Bean_weathdata> call, Response<Bean_weathdata> response) {
                if (response.code()==200){
                    List<Bean_weathdata.ROWSDETAILBean> rows_detail = response.body().getROWS_DETAIL();
                    String temperature = rows_detail.get(1).getTemperature();
                    String type = rows_detail.get(1).getType();
                    daytemptext.setText(temperature);
                    typetext.setText(type);
                    switch (type){
                        case "晴":
                            typeimage.setImageResource(R.mipmap.sun);
                            break;
                        case "小雨":
                            typeimage.setImageResource(R.mipmap.rain);
                            break;
                        case "阴":
                            typeimage.setImageResource(R.mipmap.cloudy_);
                            break;
                        default:
                            typeimage.setImageResource(R.mipmap.cloudy);
                            break;
                    }
                }
            }

            @Override
            public void onFailure(Call<Bean_weathdata> call, Throwable t) {
                Toast.makeText(getContext(),"error",Toast.LENGTH_SHORT).show();
            }
        });
    }



    private void initview(View view) {
        typeimage = (ImageView) view.findViewById(R.id.typeimage);
        daytemptext = (TextView) view.findViewById(R.id.daytemptext);
        typetext = (TextView) view.findViewById(R.id.typetext);
        timetext = (TextView) view.findViewById(R.id.timetext);
        pmtext = (TextView) view.findViewById(R.id.pmtext);
        co2text = (TextView) view.findViewById(R.id.co2text);
        temptext = (TextView) view.findViewById(R.id.temptext);
        humitext = (TextView) view.findViewById(R.id.humitext);
        designPieChart = (PieChart) view.findViewById(R.id.designpieChart);
        tips = (TextView) view.findViewById(R.id.tips);
        timetext.setText(gettime());
        ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 1, 0.5f);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        animation.setRepeatCount(150);
        typeimage.setAnimation(animation);
    }
    public String gettime(){
        String time = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
        return time;
    }

    @Override
    protected Object requestData() {
        return Constant.STATE_SUCCEED;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDestroy() {
        if (instance != null) {
            instance = null;
        }
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }

}
